#!/bin/bash

echo "EbbFlow TPS Diagnosis"
echo "===================="

# Clean logs
rm -rf logs/*
mkdir -p logs

echo "Starting EbbFlow with detailed logging..."
./bin/ebbflow_server -id 0 -n 6 -f 1 > logs/server0.log 2>&1 &
SERVER_PID=$!

sleep 5

echo "Starting client with batch=1 for detailed analysis..."
./bin/ebbflow_client -id 100 -batch 1 -count 100 -txsize 250 > logs/client.log 2>&1 &
CLIENT_PID=$!

sleep 20

# Stop processes
kill $CLIENT_PID 2>/dev/null
kill $SERVER_PID 2>/dev/null
sleep 2

echo ""
echo "=== TPS Analysis ==="
echo ""

echo "Client Log Analysis:"
echo "-------------------"
if [ -f "logs/client.log" ]; then
    echo "Transaction submission log:"
    grep -E "(Starting transaction|Submitted|TPS|Submission)" logs/client.log
    
    echo ""
    echo "Message processing log:"
    grep -E "(processing message|Node.*processing)" logs/client.log | head -10
    
    echo ""
    echo "Performance metrics:"
    grep -E "(Performance|TPS|throughput)" logs/client.log
else
    echo "Client log not found"
fi

echo ""
echo "Server Log Analysis:"
echo "-------------------"
if [ -f "logs/server0.log" ]; then
    echo "Server startup:"
    head -5 logs/server0.log
    
    echo ""
    echo "Message handling:"
    grep -E "(processing|handling|consensus)" logs/server0.log | head -10
    
    echo ""
    echo "Consensus completion:"
    grep -E "(completed consensus|consensus.*completed)" logs/server0.log
else
    echo "Server log not found"
fi

echo ""
echo "=== TPS Bottleneck Analysis ==="
echo ""

echo "Potential bottlenecks:"
echo "1. Message processing delays"
echo "2. Consensus completion rate"
echo "3. Network simulation delays"
echo "4. Lock contention"
echo "5. Channel buffer sizes"
echo "6. Epoch progression rate"

echo ""
echo "Current configuration:"
echo "- Batch size: 1"
echo "- Network delay: 100μs"
echo "- Rate limiting: 100μs"
echo "- Channel buffer: 1000"
echo "- Transaction size: 250 bytes"

echo ""
echo "Diagnosis completed."





